<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />JVM
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/jvm%E5%AD%A6%E4%B9%A003-%E5%B8%B8%E7%94%A8java%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%8F%82%E6%95%B0/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">2268 字 ~11分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h1 id="jvm学习03-常用java虚拟机参数">JVM学习03-常用Java虚拟机参数</h1>

<h2 id="一-垃圾回收日志参数">一、垃圾回收日志参数</h2>

<ol>
<li><code>-XX:+PrintGC</code> 打印简单GC日志</li>
</ol>

<p>只要GC就会打印日志。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   <span style="color:#ce5c00;font-weight:bold">[</span>GC 1023K-&gt;565K<span style="color:#ce5c00;font-weight:bold">(</span>5632K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0012699 secs<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#8f5902;font-style:italic">#日志说明：GC前堆空间使用量为1023K，GC后堆空间使用量为565K，当前可用堆空间的总和为5632K，本次GC时间为0.0012699 secs</span></code></pre></div>
<ol>
<li><p><code>-XX:+PrintGCDetails</code> 打印详细GC日志</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#ce5c00;font-weight:bold">[</span>GC<span style="color:#ce5c00;font-weight:bold">[</span>DefNew:9791K-&gt;9791K<span style="color:#ce5c00;font-weight:bold">(</span>9792K<span style="color:#ce5c00;font-weight:bold">)</span>,0.0000350 secs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">[</span>Tenured:16632K-&gt;13533K<span style="color:#ce5c00;font-weight:bold">(</span>21888K<span style="color:#ce5c00;font-weight:bold">)</span>,0.4063120 secs<span style="color:#ce5c00;font-weight:bold">]</span> 26424K-&gt;13533k<span style="color:#ce5c00;font-weight:bold">(</span>31680K<span style="color:#ce5c00;font-weight:bold">)</span>,<span style="color:#ce5c00;font-weight:bold">[</span>Perm : 2583k-&gt;2583k<span style="color:#ce5c00;font-weight:bold">(</span>21248K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span>,0.4064710 secs <span style="color:#ce5c00;font-weight:bold">[</span>Times:user<span style="color:#ce5c00;font-weight:bold">=</span>0.41 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.40 secs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">]</span>
<span style="color:#8f5902;font-style:italic">#日志说明：</span>
<span style="color:#8f5902;font-style:italic">#[DefNew:9791K-&gt;9791K(9792K),0.0000350 secs] 新生代回收</span>
<span style="color:#8f5902;font-style:italic">#[Tenured:16632K-&gt;13533K(21888K),0.4063120 secs] 老年代回收</span>
<span style="color:#8f5902;font-style:italic">#26424K-&gt;13533k(31680K) 堆回收:由GC前的26M到GC后的13M，堆总可用变为31M，但是这里要注意，堆回收了13M，但是老年代只回收了3M，剩下的其实是新生代的内存回收，虽然日志里面显示着新生代没有回收，但是实际是被清空了的。</span>
<span style="color:#8f5902;font-style:italic">#[Perm : 2583k-&gt;2583k(21248K)] 永久区回收</span>
   
<span style="color:#8f5902;font-style:italic">#以上的日志是书上的日志，我自己的没有老年代和永久区的日志，而且年轻代的名称也不是DefNew，而是PSYoungGen</span>
<span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen: 2047K-&gt;512K<span style="color:#ce5c00;font-weight:bold">(</span>2560K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 2047K-&gt;520K<span style="color:#ce5c00;font-weight:bold">(</span>9728K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0009979 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
<span style="color:#0000cf;font-weight:bold">6</span>
Heap
PSYoungGen      total 2560K, used 1243K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000, 0x00000007c0000000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
 eden space 2048K, 35% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000,0x00000007bfdb6e98,0x00000007bff00000<span style="color:#ce5c00;font-weight:bold">)</span>
 from space 512K, 100% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff00000,0x00000007bff80000,0x00000007bff80000<span style="color:#ce5c00;font-weight:bold">)</span>
 to   space 512K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff80000,0x00000007bff80000,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
ParOldGen       total 7168K, used 8K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf600000, 0x00000007bfd00000, 0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
 object space 7168K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf600000,0x00000007bf602000,0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
Metaspace       used 2709K, capacity 4486K, committed 4864K, reserved 1056768K
 class space    used 289K, capacity 386K, committed 512K, reserved 1048576K
   </code></pre></div></li>

<li><p><code>-XX:+PrintHeapAtGC</code> 分别在每次GC前后分别打印堆信息。效果如下</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#ce5c00;font-weight:bold">{</span>Heap before GC <span style="color:#000">invocations</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">1</span> <span style="color:#ce5c00;font-weight:bold">(</span>full 0<span style="color:#ce5c00;font-weight:bold">)</span>:
PSYoungGen      total 2560K, used 2047K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000, 0x00000007c0000000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
 eden space 2048K, 99% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000,0x00000007bfeffff0,0x00000007bff00000<span style="color:#ce5c00;font-weight:bold">)</span>
 from space 512K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff80000,0x00000007bff80000,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
 to   space 512K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff00000,0x00000007bff00000,0x00000007bff80000<span style="color:#ce5c00;font-weight:bold">)</span>
ParOldGen       total 7168K, used 0K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf600000, 0x00000007bfd00000, 0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
 object space 7168K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf600000,0x00000007bf600000,0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
Metaspace       used 2700K, capacity 4486K, committed 4864K, reserved 1056768K
 class space    used 288K, capacity 386K, committed 512K, reserved 1048576K
<span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen: 2047K-&gt;512K<span style="color:#ce5c00;font-weight:bold">(</span>2560K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 2047K-&gt;520K<span style="color:#ce5c00;font-weight:bold">(</span>9728K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0007641 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
Heap after GC <span style="color:#000">invocations</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">1</span> <span style="color:#ce5c00;font-weight:bold">(</span>full 0<span style="color:#ce5c00;font-weight:bold">)</span>:
PSYoungGen      total 2560K, used 512K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000, 0x00000007c0000000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
 eden space 2048K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000,0x00000007bfd00000,0x00000007bff00000<span style="color:#ce5c00;font-weight:bold">)</span>
 from space 512K, 100% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff00000,0x00000007bff80000,0x00000007bff80000<span style="color:#ce5c00;font-weight:bold">)</span>
 to   space 512K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff80000,0x00000007bff80000,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
ParOldGen       total 7168K, used 8K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf600000, 0x00000007bfd00000, 0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
 object space 7168K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf600000,0x00000007bf602000,0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
Metaspace       used 2700K, capacity 4486K, committed 4864K, reserved 1056768K
 class space    used 288K, capacity 386K, committed 512K, reserved 1048576K
<span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div></li>

<li><p><code>-XX:+PrintGCTimeStamps</code> 输出GC的发生时间，时间为虚拟机启动后的时间偏移量。相当于<code>-XX:+PrintGCDetails</code> 加了个时间</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#8f5902;font-style:italic"># 这个0.123就是时间偏移量，虚拟机启动后0.123秒发生了GC</span>
0.123: <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen: 2047K-&gt;512K<span style="color:#ce5c00;font-weight:bold">(</span>2560K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 2047K-&gt;520K<span style="color:#ce5c00;font-weight:bold">(</span>9728K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0015470 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
<span style="color:#0000cf;font-weight:bold">7</span>
Heap
PSYoungGen      total 2560K, used 1353K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000, 0x00000007c0000000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
 eden space 2048K, 41% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000,0x00000007bfdd25e8,0x00000007bff00000<span style="color:#ce5c00;font-weight:bold">)</span>
 from space 512K, 100% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff00000,0x00000007bff80000,0x00000007bff80000<span style="color:#ce5c00;font-weight:bold">)</span>
 to   space 512K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff80000,0x00000007bff80000,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
ParOldGen       total 7168K, used 8K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf600000, 0x00000007bfd00000, 0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
 object space 7168K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf600000,0x00000007bf602000,0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
Metaspace       used 2708K, capacity 4486K, committed 4864K, reserved 1056768K
 class space    used 289K, capacity 386K, committed 512K, reserved 1048576K</code></pre></div></li>

<li><p><code>-XX:+PrintGCApplicationConcurrentTime</code> 打印应用程序的执行时间</p></li>

<li><p><code>-XX:+PrintGCApplicationStoppedTime</code> 打印应用程序由于GC而产生的停顿时间。</p></li>

<li><p><code>-XX:+PrintReferenceGC</code> 跟踪系统内的软引用、弱引用、虚引用Finallize队列。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">0.115: Application time: 0.0382099 seconds
0.115: [GC (Allocation Failure) 0.116: [SoftReference, 0 refs, 0.0000373 secs]0.116: [WeakReference, 9 refs, 0.0000084 secs]0.116: [FinalReference, 62 refs, 0.0000349 secs]0.116: [PhantomReference, 0 refs, 0 refs, 0.0000195 secs]0.116: [JNI Weak Reference, 0.0000093 secs][PSYoungGen: 2047K-&gt;496K(2560K)] 2047K-&gt;528K(9728K), 0.0018549 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
0.116: Total time for which application threads were stopped: 0.0019804 seconds, Stopping threads took: 0.0000121 seconds</code></pre></div>
<p>Java从1.2版本开始引入了4种引用，这4种引用的级别由高到低依次为：</p>

<p><strong>强引用  &gt;  软引用  &gt;  弱引用  &gt;  虚引用</strong></p></li>
</ol>

<p><strong>(1)强引用（StrongReference）</strong>
     强引用是使用最普遍的引用。如果一个对象具有强引用，那垃圾回收器绝不会回收它。当内存空间不足，Java虚拟机宁愿抛出OutOfMemoryError错误，使程序异常终止，也不会靠随意回收具有强引用的对象来解决内存不足的问题。</p>

<p><strong>(2)软引用（SoftReference）</strong></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> 如果一个对象只具有软引用，则内存空间足够，垃圾回收器就不会回收它；如果内存空间不足了，就会回收这些对象的内存。只要垃圾回收器没有回收它，该对象就可以被程序使用。软引用可用来实现内存敏感的高速缓存。

 软引用可以和一个引用队列（ReferenceQueue）联合使用，如果软引用所引用的对象被垃圾回收器回收，Java虚拟机就会把这个软引用加入到与之关联的引用队列中。</code></pre></div>
<p><strong>(3)弱引用（WeakReference）</strong></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> 弱引用与软引用的区别在于：只具有弱引用的对象拥有更短暂的生命周期。在垃圾回收器线程扫描它所管辖的内存区域的过程中，一旦发现了只具有弱引用的对象，不管当前内存空间足够与否，都会回收它的内存。不过，由于垃圾回收器是一个优先级很低的线程，因此不一定会很快发现那些只具有弱引用的对象。

 弱引用可以和一个引用队列（ReferenceQueue）联合使用，如果弱引用所引用的对象被垃圾回收，Java虚拟机就会把这个弱引用加入到与之关联的引用队列中。</code></pre></div>
<p><strong>(4)虚引用（PhantomReference）</strong></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback"> “虚引用”顾名思义，就是形同虚设，与其他几种引用都不同，虚引用并不会决定对象的生命周期。如果一个对象仅持有虚引用，那么它就和没有任何引用一样，在任何时候都可能被垃圾回收器回收。

 虚引用主要用来跟踪对象被垃圾回收器回收的活动。虚引用与软引用和弱引用的一个区别在于：虚引用必须和引用队列 （ReferenceQueue）联合使用。当垃圾回收器准备回收一个对象时，如果发现它还有虚引用，就会在回收对象的内存之前，把这个虚引用加入到与之 关联的引用队列中。</code></pre></div>
<p><strong>(5)FinalReference</strong></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">对于重载了 Object 类的 finalize 方法的类实例化的对象（这里称为 f 对象），JVM 为了能在 GC 对象时触发 f 对象的 finalize 方法的调用，将每个 f 对象包装生成一个对应的FinalReference 对象，方便 GC 时进行处理。</code></pre></div>
<p>FinalReference说明：<a href="http://lovestblog.cn/blog/2015/07/09/final-reference/">FinalReference</a></p>

<ol>
<li><code>-Xloggc</code> 指定日志目录。 <code>-Xloggc:log/gc.log</code></li>
</ol>

<h2 id="二-类加载-制裁的跟踪">二、类加载/制裁的跟踪</h2>

<ol>
<li><code>-verbos:class</code> 跟踪类的加载和卸载</li>
</ol>

<p><code>-XX:+TraceClassLoading</code>跟踪类加载，动态类的加载非常隐蔽，它们由代码逻辑控制，不出现在文件系统中，跟踪这些类，就需要使用<code>-XX:+TraceClassLoading</code>等参数来观察系统实际使用的类。</p>

<p><code>-XX:+TraceClassUnloading</code> 跟踪类的卸载</p>

<p><code>-verbos:class</code> = <code>-XX:+TraceClassLoading</code> + <code>-XX:+TraceClassUnloading</code></p>

<p>测试代码：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">   <span style="color:#204a87;font-weight:bold">package</span> <span style="color:#000">cn.shutdown.demo.jvm.trace</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org.objectweb.asm.ClassWriter</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org.objectweb.asm.MethodVisitor</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org.objectweb.asm.Opcodes</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java.lang.reflect.InvocationTargetException</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java.lang.reflect.Method</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">    * -XX:+TraceClassUnloading -XX:+TraceClassLoading
</span><span style="color:#8f5902;font-style:italic">    * &lt;p&gt;
</span><span style="color:#8f5902;font-style:italic">    * -verbose:class
</span><span style="color:#8f5902;font-style:italic">    *
</span><span style="color:#8f5902;font-style:italic">    * @author Dmn
</span><span style="color:#8f5902;font-style:italic">    */</span>
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">UnloadClass</span> <span style="color:#204a87;font-weight:bold">implements</span> <span style="color:#000">Opcodes</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">throws</span> <span style="color:#000">NoSuchMethodException</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">SecurityException</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">IllegalAccessException</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">IllegalArgumentException</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">InvocationTargetException</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#8f5902;font-style:italic">//ClassWriter 以字节码的形式生成类的类访问器, 参数
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#8f5902;font-style:italic">// ClassWriter.COMPUTE_MAXS 如果必须自动计算最大堆栈大小和局部变量数，则为true 。
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#8f5902;font-style:italic">// ClassWriter.COMPUTE_FRAMES 如果堆栈映射帧必须从头开始重新计算。
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">ClassWriter</span> <span style="color:#000">cw</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">ClassWriter</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">ClassWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">COMPUTE_MAXS</span> <span style="color:#ce5c00;font-weight:bold">|</span> <span style="color:#000">ClassWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">COMPUTE_FRAMES</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#8f5902;font-style:italic">// 定义了一个 基于jdk1.7的 public类型的类，名为Example,继承于Object
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">cw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visit</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">V1_7</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">ACC_PUBLIC</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;Example&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;java/lang/Object&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#8f5902;font-style:italic">//定义了一个构造方法
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">MethodVisitor</span> <span style="color:#000">mw</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">cw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">ACC_PUBLIC</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;&lt;init&gt;&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;()V&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitVarInsn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">ALOAD</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitMethodInsn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">INVOKESPECIAL</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;java/lang/Object&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;&lt;init&gt;&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;()V&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitInsn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">RETURN</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitMaxs</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitEnd</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
           <span style="color:#8f5902;font-style:italic">// 生成main方法中的字节码指令
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">mw</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">cw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">ACC_PUBLIC</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">ACC_STATIC</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;main&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;([Ljava/lang/String;)V&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#8f5902;font-style:italic">//获取该方法
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitFieldInsn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">GETSTATIC</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;java/lang/System&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;out&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;Ljava/io/PrintStream;&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#8f5902;font-style:italic">//加载字符串参数
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitLdcInsn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Hello world!&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#8f5902;font-style:italic">//调用该方法
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitMethodInsn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">INVOKEVIRTUAL</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;java/io/PrintStream&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;println&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;(Ljava/lang/String;)V&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitInsn</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">RETURN</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitMaxs</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">mw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">visitEnd</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#8f5902;font-style:italic">//生成class文件对应的二进制流
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">code</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">cw</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">toByteArray</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;\n\n================================================&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">10</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
               <span style="color:#8f5902;font-style:italic">//创建类加载器
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#000">UnloadClassLoader</span> <span style="color:#000">loader</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">UnloadClassLoader</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
               <span style="color:#8f5902;font-style:italic">//获取了 ClassLoader类的 defineClass方法对象
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#000">Method</span> <span style="color:#000">m</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ClassLoader</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">getDeclaredMethod</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;defineClass&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">int</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
               <span style="color:#8f5902;font-style:italic">//设置方法的访问权限为可访问
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#000">m</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">setAccessible</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">true</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
               <span style="color:#8f5902;font-style:italic">//调用 loader对象的 defineClass方法
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#8f5902;font-style:italic">//ClassLoader的defineClass方法的作用是：将字节数组转换为类Class的实例
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#8f5902;font-style:italic">//这样就可以将 刚刚生成的class文件的二进制流加载并转化为Example类的实例
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#000">m</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">invoke</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">loader</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;Example&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">code</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">code</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">length</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
               <span style="color:#000">m</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">setAccessible</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">false</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
               <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">gc</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   <span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div><div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">   <span style="color:#204a87;font-weight:bold">package</span> <span style="color:#000">cn.shutdown.demo.jvm.trace</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">UnloadClassLoader</span> <span style="color:#204a87;font-weight:bold">extends</span> <span style="color:#000">ClassLoader</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   <span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div>
<p>需要引用的pom依赖</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">   &lt;dependency&gt;
     &lt;groupId&gt;org.ow2.asm&lt;/groupId&gt;
     &lt;artifactId&gt;asm&lt;/artifactId&gt;
     &lt;version&gt;5.0.4&lt;/version&gt;
   &lt;/dependency&gt;
   &lt;dependency&gt;
     &lt;groupId&gt;org.ow2.asm&lt;/groupId&gt;
     &lt;artifactId&gt;asm-commons&lt;/artifactId&gt;
     &lt;version&gt;5.0.4&lt;/version&gt;
   &lt;/dependency&gt;</code></pre></div>
<p>关于ASM 参考文章，写的非常清晰 <a href="https://my.oschina.net/liboware/blog/5005975">Java技术专题-JVM研究系列（3）ASM库生成和修改class文件</a></p>

<p>运行后的效果：</p>

<p>可以看出日志输出中有引的加载和卸载的日志记录</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   <span style="color:#ce5c00;font-weight:bold">[</span>Opened /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded java.lang.Object from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded java.io.Serializable from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded java.lang.Comparable from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   。。。省略部分输出。。。
   
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded cn.shutdown.demo.jvm.trace.UnloadClassLoader from file:/Users/dmn/IdeaProjects/demo/target/classes/<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded java.lang.ClassFormatError from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded java.io.IOException from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded java.lang.AssertionStatusDirectives from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded sun.reflect.NativeMethodAccessorImpl from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded sun.reflect.DelegatingMethodAccessorImpl from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#8f5902;font-style:italic">#这里就开始循环中的输出了，从JVM定义的类中加载了Example</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded Example from __JVM_DefineClass__<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded Example from __JVM_DefineClass__<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#8f5902;font-style:italic">#从JVM定义的类中卸Example</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Unloading class Example 0x00000007c006a028<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded Example from __JVM_DefineClass__<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Unloading class Example 0x00000007c006a828<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded Example from __JVM_DefineClass__<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Unloading class Example 0x00000007c006a028<span style="color:#ce5c00;font-weight:bold">]</span>
   。。。活力部分输出。。。
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded java.lang.Shutdown from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Loaded java.lang.Shutdown<span style="color:#000">$Lock</span> from /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/rt.jar<span style="color:#ce5c00;font-weight:bold">]</span></code></pre></div>
<h2 id="三-系统参数查看">三、系统参数查看</h2>

<ol>
<li><p><code>-XX:+PrintVMOptions</code>打印虚拟机接受到的命令行的显式参数</p></li>

<li><p><code>-XX:+PrintCommandLineFlags</code> 打印传递给虚拟机的显式和隐式参数(隐式参数未必通过命令行给出 可能由虚拟机自行设置)</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">-XX:InitialHeapSize<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">268435456</span> -XX:MaxHeapSize<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">4294967296</span> -XX:+PrintClassHistogram - XX:+PrintCommandLineFlags -XX:+PrintFlagsFinal -XX:+PrintVMOptions -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 
<span style="color:#8f5902;font-style:italic">#-XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4294967296 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC  这些都是隐式参数</span>
   
   </code></pre></div></li>

<li><p><code>-XX:+PrintFlagsFinal</code> 查看系统的详细参数。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#ce5c00;font-weight:bold">[</span>Global flags<span style="color:#ce5c00;font-weight:bold">]</span>
    intx <span style="color:#000">ActiveProcessorCount</span>                      <span style="color:#ce5c00;font-weight:bold">=</span> -1                                  <span style="color:#ce5c00;font-weight:bold">{</span>product<span style="color:#ce5c00;font-weight:bold">}</span>
   uintx <span style="color:#000">AdaptiveSizeDecrementScaleFactor</span>          <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">4</span>                                   <span style="color:#ce5c00;font-weight:bold">{</span>product<span style="color:#ce5c00;font-weight:bold">}</span>
   uintx <span style="color:#000">AdaptiveSizeMajorGCDecayTimeScale</span>         <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">10</span>                                  <span style="color:#ce5c00;font-weight:bold">{</span>product<span style="color:#ce5c00;font-weight:bold">}</span>
   uintx <span style="color:#000">AdaptiveSizePausePolicy</span>                   <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">0</span>                                   <span style="color:#ce5c00;font-weight:bold">{</span>product<span style="color:#ce5c00;font-weight:bold">}</span>
   uintx <span style="color:#000">AdaptiveSizePolicyCollectionCostMargin</span>    <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">50</span>                                  <span style="color:#ce5c00;font-weight:bold">{</span>product<span style="color:#ce5c00;font-weight:bold">}</span>
   uintx <span style="color:#000">AdaptiveSizePolicyInitializingSteps</span>       <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">20</span>                                  <span style="color:#ce5c00;font-weight:bold">{</span>product<span style="color:#ce5c00;font-weight:bold">}</span>
   
。。。。省略大部分。。。</code></pre></div></li>
</ol>

<h2 id="四-堆参数配置">四、堆参数配置</h2>

<ol>
<li>最大堆和初始堆设置</li>
</ol>

<p><code>-Xms</code>:初始堆</p>

<p><code>-Xmx</code>:最大堆</p>

<p>测试代码：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">   <span style="color:#204a87;font-weight:bold">package</span> <span style="color:#000">cn.shutdown.demo.jvm</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">    * -Xmx20m -Xms5m -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseSerialGC
</span><span style="color:#8f5902;font-style:italic">    * @author dmn
</span><span style="color:#8f5902;font-style:italic">    * @date 2021/6/7
</span><span style="color:#8f5902;font-style:italic">    */</span>
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">HeapAlloc</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   
           <span style="color:#000">printMemory</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#8f5902;font-style:italic">//分配1M内存
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#000">1</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">1024</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">1024</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">printMemory</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;分配了1M内存&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#8f5902;font-style:italic">//分配4M内存
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#000">4</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">1024</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">1024</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">printMemory</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;分配了4M内存&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   
       <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">printMemory</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span> <span style="color:#000">step</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">step</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;maxMemory=&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">Runtime</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">getRuntime</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">maxMemory</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34; bytes&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;freeMemory=&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">Runtime</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">getRuntime</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">freeMemory</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34; bytes&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;toatlMemory=&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">Runtime</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">getRuntime</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">totalMemory</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34; bytes&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   
   <span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div>
<p>运行结果：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   -XX:InitialHeapSize<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">5242880</span> -XX:MaxHeapSize<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">20971520</span> -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseSerialGC 
   
   <span style="color:#000">maxMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">20316160</span> bytes
   <span style="color:#000">freeMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">5287536</span> bytes
   <span style="color:#000">toatlMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">6094848</span> bytes
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>DefNew: 788K-&gt;192K<span style="color:#ce5c00;font-weight:bold">(</span>1856K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0012785 secs<span style="color:#ce5c00;font-weight:bold">]</span> 788K-&gt;363K<span style="color:#ce5c00;font-weight:bold">(</span>5952K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0013054 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   分配了1M内存
   <span style="color:#000">maxMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">20316160</span> bytes
   <span style="color:#000">freeMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">4640168</span> bytes
   <span style="color:#000">toatlMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">6094848</span> bytes
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>DefNew: 1249K-&gt;0K<span style="color:#ce5c00;font-weight:bold">(</span>1856K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0016099 secs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">[</span>Tenured: 1387K-&gt;1387K<span style="color:#ce5c00;font-weight:bold">(</span>4096K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0015703 secs<span style="color:#ce5c00;font-weight:bold">]</span> 1420K-&gt;1387K<span style="color:#ce5c00;font-weight:bold">(</span>5952K<span style="color:#ce5c00;font-weight:bold">)</span>, <span style="color:#ce5c00;font-weight:bold">[</span>Metaspace: 2697K-&gt;2697K<span style="color:#ce5c00;font-weight:bold">(</span>1056768K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span>, 0.0032377 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   分配了4M内存
   <span style="color:#000">maxMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">20316160</span> bytes
   <span style="color:#000">freeMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">4708856</span> bytes
   <span style="color:#000">toatlMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10358784</span> bytes
   Heap
    def new generation   total 1920K, used 69K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000, 0x00000007bee10000, 0x00000007bf2a0000<span style="color:#ce5c00;font-weight:bold">)</span>
     eden space 1728K,   4% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000, 0x00000007bec11498, 0x00000007bedb0000<span style="color:#ce5c00;font-weight:bold">)</span>
     from space 192K,   0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bedb0000, 0x00000007bedb0000, 0x00000007bede0000<span style="color:#ce5c00;font-weight:bold">)</span>
     to   space 192K,   0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bede0000, 0x00000007bede0000, 0x00000007bee10000<span style="color:#ce5c00;font-weight:bold">)</span>
    tenured generation   total 8196K, used 5483K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf2a0000, 0x00000007bfaa1000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
      the space 8196K,  66% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf2a0000, 0x00000007bf7fad20, 0x00000007bf7fae00, 0x00000007bfaa1000<span style="color:#ce5c00;font-weight:bold">)</span>
    Metaspace       used 2703K, capacity 4486K, committed 4864K, reserved 1056768K
     class space    used 289K, capacity 386K, committed 512K, reserved 1048576K
   
   Process finished with <span style="color:#204a87">exit</span> code <span style="color:#0000cf;font-weight:bold">0</span>
   
   测试代码运行的初始堆是5m，最大堆是20m，程序运行以后
   1. 第一次查看的内存结果
   <span style="color:#000">maxMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">20316160</span> bytes
   <span style="color:#000">freeMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">5287536</span> bytes
   <span style="color:#000">toatlMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">6094848</span> bytes
   2. 分配了1M内存以后，freeMemory减少了1M，变为4640168 bytes
   <span style="color:#000">freeMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">4640168</span> bytes
   进行了一次垃圾回收的结果
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> 
   <span style="color:#ce5c00;font-weight:bold">[</span>DefNew: 1249K-&gt;0K<span style="color:#ce5c00;font-weight:bold">(</span>1856K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0016099 secs<span style="color:#ce5c00;font-weight:bold">]</span> 新生代可用空间为 1856K（大约是1.5M）
   <span style="color:#ce5c00;font-weight:bold">[</span>Tenured: 1387K-&gt;1387K<span style="color:#ce5c00;font-weight:bold">(</span>4096K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0015703 secs<span style="color:#ce5c00;font-weight:bold">]</span> 老年代可用空间为 4096K （4M）
   1420K-&gt;1387K<span style="color:#ce5c00;font-weight:bold">(</span>5952K<span style="color:#ce5c00;font-weight:bold">)</span> 堆的总可用空间为 5952K（近6M<span style="color:#ce5c00;font-weight:bold">)</span>
   <span style="color:#ce5c00;font-weight:bold">[</span>Metaspace: 2697K-&gt;2697K<span style="color:#ce5c00;font-weight:bold">(</span>1056768K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span>, 0.0032377 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   3. 分配4M内存后
   因为从刚刚的GC记录可以看到，新生代的可用空间只有1.5M了，小于程序申请的4M空间，因此堆空间进行扩容，扩容后，总内存为约10M，剩余内存为 4708856，约5M
   <span style="color:#000">maxMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">20316160</span> bytes
   <span style="color:#000">freeMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">4708856</span> bytes
   <span style="color:#000">toatlMemory</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10358784</span> bytes
   </code></pre></div>
<p>在实际工作中，也可以直接将初始堆 -Xms与最大堆 -Xmx设置相等，好处是可以减少程序运行时进行垃圾回收的次数，从而提高程序的性能。</p>

<ol>
<li><p>新生代配置</p>

<ul>
<li><p><code>-Xmn</code>:设置新生代大小。设置较大的新生代会减少老年代的大小，这个参数对系统性能及GC行为有很大影响，新生代大小一般设置为整个堆空间的1/3到1/4左右。</p></li>

<li><p><code>-XX:SurvivorRatio</code>:设置新生代中eden空间和from/to空间的比例关系。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">含义：
-XX:SurvivorRatio<span style="color:#ce5c00;font-weight:bold">=</span>eden/from<span style="color:#ce5c00;font-weight:bold">=</span>eden/to
使用方法：
-XX:SurvivorRatio<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">2</span></code></pre></div></li>

<li><p><code>-XX:NewRatio</code>:设置新生代和老年代的比例</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">-XX:NewRatio<span style="color:#ce5c00;font-weight:bold">=</span>老年代/新生代</code></pre></div></li>
</ul></li>
</ol>

<p>测试代码：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">   <span style="color:#204a87;font-weight:bold">package</span> <span style="color:#000">cn.shutdown.demo.jvm</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">    * -Xmx20m -Xms20m -Xmn1m -XX:SurvivorRatio=2 -XX:+PrintGCDetails
</span><span style="color:#8f5902;font-style:italic">    * @author dmn
</span><span style="color:#8f5902;font-style:italic">    * @date 2021/6/15
</span><span style="color:#8f5902;font-style:italic">    */</span>
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">NewSizeDemo</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">10</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
               <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#000">1</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">1024</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">1024</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   <span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div>
<p>运行结果：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   Java HotSpot<span style="color:#ce5c00;font-weight:bold">(</span>TM<span style="color:#ce5c00;font-weight:bold">)</span> 64-Bit Server VM warning: NewSize <span style="color:#ce5c00;font-weight:bold">(</span>1536k<span style="color:#ce5c00;font-weight:bold">)</span> is greater than the MaxNewSize <span style="color:#ce5c00;font-weight:bold">(</span>1024k<span style="color:#ce5c00;font-weight:bold">)</span>. A new max generation size of 1536k will be used.
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen: 512K-&gt;496K<span style="color:#ce5c00;font-weight:bold">(</span>1024K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 512K-&gt;512K<span style="color:#ce5c00;font-weight:bold">(</span>19968K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0015693 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   Heap
    PSYoungGen      total 1024K, used 738K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfe80000, 0x00000007c0000000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
     eden space 512K, 47% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfe80000,0x00000007bfebc8d0,0x00000007bff00000<span style="color:#ce5c00;font-weight:bold">)</span>
     from space 512K, 96% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff00000,0x00000007bff7c010,0x00000007bff80000<span style="color:#ce5c00;font-weight:bold">)</span>
     to   space 512K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff80000,0x00000007bff80000,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
    ParOldGen       total 18944K, used 10256K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000, 0x00000007bfe80000, 0x00000007bfe80000<span style="color:#ce5c00;font-weight:bold">)</span>
     object space 18944K, 54% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000,0x00000007bf6040a0,0x00000007bfe80000<span style="color:#ce5c00;font-weight:bold">)</span>
    Metaspace       used 2700K, capacity 4486K, committed 4864K, reserved 1056768K
     class space    used 289K, capacity 386K, committed 512K, reserved 1048576K</code></pre></div>
<p>1) 从结果里我们可以看到 ，青年代虽然输出总大小为1024k，但是 eden,from,to的空间分别都为 512k，这个的原因可以看下我的另一篇文章<a href="https://blog.csdn.net/wejack/article/details/117928203">Java HotSpot&trade; 64-Bit Server VM warning: NewSize (1536k) is greater than the MaxNewSize (1024k) </a> 里的说明，是jdk7与jdk8的区别导致的，jdk8的青年代的最小值为1536k，因为参数给定的值是1024k，所以被默认设置为了1536k，正好是eden,from,to各512k。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">    PSYoungGen      total 1024K, used 738K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfe80000, 0x00000007c0000000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
     eden space 512K, 47% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfe80000,0x00000007bfebc8d0,0x00000007bff00000<span style="color:#ce5c00;font-weight:bold">)</span>
     from space 512K, 96% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff00000,0x00000007bff7c010,0x00000007bff80000<span style="color:#ce5c00;font-weight:bold">)</span>
     to   space 512K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff80000,0x00000007bff80000,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
    ParOldGen       total 18944K, used 10256K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000, 0x00000007bfe80000, 0x00000007bfe80000<span style="color:#ce5c00;font-weight:bold">)</span> </code></pre></div>
<p>另外，由于eden区无法容纳任何一个程序中分配的1MB的数组，所以触发了一次新生代的GC，对eden区进行了部分回收，同时，这个偏小的新生代无法为1MB数组预留空间，所以，所有的数组都分配在了老年代，老年代最终占用了10256K的空间。</p>

<p>2) 上述测试代码如果使用<code>-Xmx20m -Xms20m -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails</code> 的JVM参数来运行的话，结果如下。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   分配1M内存
   分配1M内存
   分配1M内存
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen: 3900K-&gt;1520K<span style="color:#ce5c00;font-weight:bold">(</span>5632K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 3900K-&gt;1560K<span style="color:#ce5c00;font-weight:bold">(</span>18944K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0016175 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   分配1M内存
   分配1M内存
   分配1M内存
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen: 4672K-&gt;1520K<span style="color:#ce5c00;font-weight:bold">(</span>5632K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 4712K-&gt;1568K<span style="color:#ce5c00;font-weight:bold">(</span>18944K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0013520 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   分配1M内存
   分配1M内存
   分配1M内存
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen: 4663K-&gt;1520K<span style="color:#ce5c00;font-weight:bold">(</span>5632K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 4711K-&gt;1568K<span style="color:#ce5c00;font-weight:bold">(</span>18944K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0007291 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   分配1M内存
   Heap
    PSYoungGen      total 5632K, used 2626K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf900000, 0x00000007c0000000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
     eden space 4096K, 27% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf900000,0x00000007bfa14990,0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
     from space 1536K, 98% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000,0x00000007bfe7c020,0x00000007bfe80000<span style="color:#ce5c00;font-weight:bold">)</span>
     to   space 1536K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfe80000,0x00000007bfe80000,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
    ParOldGen       total 13312K, used 48K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000, 0x00000007bf900000, 0x00000007bf900000<span style="color:#ce5c00;font-weight:bold">)</span>
     object space 13312K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000,0x00000007bec0c000,0x00000007bf900000<span style="color:#ce5c00;font-weight:bold">)</span>
    Metaspace       used 2702K, capacity 4486K, committed 4864K, reserved 1056768K
     class space    used 289K, capacity 386K, committed 512K, reserved 1048576K
   
   Process finished with <span style="color:#204a87">exit</span> code <span style="color:#0000cf;font-weight:bold">0</span>
   </code></pre></div>
<p>从结果看出，青年代被初始值设置为7M以后，因为 <code>-XX:SurvivorRatio=2</code>，所以eden区与from区比为2/1，所以空间大小为</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">    eden space 4096K, 27% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf900000,0x00000007bfa14990,0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
    from space 1536K, 98% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000,0x00000007bfe7c020,0x00000007bfe80000<span style="color:#ce5c00;font-weight:bold">)</span>
    to   space 1536K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfe80000,0x00000007bfe80000,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span></code></pre></div>
<p>每次程序分配会分区eden区的内存，分配三次以后，eden区的内存不足了，对eden区进行部分回收。由于程序每申请一次空间，也同时废弃上一次申请的内存（上次申请的内存失去了引用），所以在新生代的GC中，有效回收了失效的内在，最终结果是：所有的内在分配都在新生代进行，通过GC保证了新生代有足够的空间，而老年代没有为这些数组预留任何空间，只是在GC过程中，部分新生代对象晋升到老年代。</p>

<p>3) 使用参数<code>-Xmx20m -Xms20m -Xmn15m -XX:SurvivorRatio=2 -XX:+PrintGCDetails</code>运行上述代码，得到的输出为：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   Heap
    PSYoungGen      total 13824K, used 11469K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf100000, 0x00000007c0000000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
     eden space 12288K, 93% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf100000,0x00000007bfc336f8,0x00000007bfd00000<span style="color:#ce5c00;font-weight:bold">)</span>
     from space 1536K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfe80000,0x00000007bfe80000,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
     to   space 1536K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bfd00000,0x00000007bfd00000,0x00000007bfe80000<span style="color:#ce5c00;font-weight:bold">)</span>
    ParOldGen       total 5120K, used 0K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000, 0x00000007bf100000, 0x00000007bf100000<span style="color:#ce5c00;font-weight:bold">)</span>
     object space 5120K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000,0x00000007bec00000,0x00000007bf100000<span style="color:#ce5c00;font-weight:bold">)</span>
    Metaspace       used 2702K, capacity 4486K, committed 4864K, reserved 1056768K
     class space    used 289K, capacity 386K, committed 512K, reserved 1048576K</code></pre></div>
<p>这次执行中，新生代初始化15M的空间，eden区占用了12288K，满足10M数组的分配 。因此所有的分配行为都在eden直接运行，且没有触发任何的GC行为，因为 from/to和老年代的使用率都为0。</p>

<blockquote>
<p>不同的堆的分布情况，对系统执行会产生一定影响。在实际工作中，应该根据系统的特点做合理的设置，基本的策略是：尽可能将对象预留在新生代，减少老年代的GC次数。</p>
</blockquote>

<p>4) 使用参数<code>-Xmx20M -Xms20M -XX:NewRatio=2 -XX:+PrintGCDetails</code>运行测试代码，输出如下:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@45ee12a7
   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@330bedb4
   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@2503dbd3
   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@4b67cf4d  发生GC的时候，这个对象与引用b还有关联，所以这个会放到from/to区，但是空间不足，所以给放到了老年代
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen: 5014K-&gt;512K<span style="color:#ce5c00;font-weight:bold">(</span>6144K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 5014K-&gt;1560K<span style="color:#ce5c00;font-weight:bold">(</span>19968K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0014339 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@7ea987ac
   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@12a3a380
   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@29453f44
   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@5cad8086
   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@6e0be858  发生GC的时候，这个对象与引用b还有关联，所以这个会放到from/to区，但是空间不足，所以给放到了老年代
   <span style="color:#ce5c00;font-weight:bold">[</span>GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">[</span>PSYoungGen: 5742K-&gt;496K<span style="color:#ce5c00;font-weight:bold">(</span>6144K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span> 6790K-&gt;2580K<span style="color:#ce5c00;font-weight:bold">(</span>19968K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.0011273 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.00 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   分配1M内存<span style="color:#ce5c00;font-weight:bold">[</span>B@61bbe9ba
   Heap
    PSYoungGen      total 6144K, used 1743K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf980000, 0x00000007c0000000, 0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
     eden space 5632K, 22% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bf980000,0x00000007bfab7df0,0x00000007bff00000<span style="color:#ce5c00;font-weight:bold">)</span>
     from space 512K, 96% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff80000,0x00000007bfffc010,0x00000007c0000000<span style="color:#ce5c00;font-weight:bold">)</span>
     to   space 512K, 0% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bff00000,0x00000007bff00000,0x00000007bff80000<span style="color:#ce5c00;font-weight:bold">)</span>
    ParOldGen       total 13824K, used 2084K <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000, 0x00000007bf980000, 0x00000007bf980000<span style="color:#ce5c00;font-weight:bold">)</span>
     object space 13824K, 15% used <span style="color:#ce5c00;font-weight:bold">[</span>0x00000007bec00000,0x00000007bee09030,0x00000007bf980000<span style="color:#ce5c00;font-weight:bold">)</span>
    Metaspace       used 2703K, capacity 4486K, committed 4864K, reserved 1056768K
     class space    used 289K, capacity 386K, committed 512K, reserved 1048576K
   
   Process finished with <span style="color:#204a87">exit</span> code <span style="color:#0000cf;font-weight:bold">0</span>
   
   </code></pre></div>
<p>堆大小为20M，老年代和新生代比为2：1，所以老年代大小为13824K，新生代大小为 6144K。新生代大小不够10M的数组分配，所以产生新生代的GC，新生代GC时，from/to空间不足以容纳任何一个1MB的数组，影响了新生代的正常回收，故新生代回收时需要老年代进行空间担保。</p>

<blockquote>
<p>默认的，新生代 ( Young ) 与老年代 ( Old ) 的比例的值为 1:2 ( 该值可以通过参数 –XX:NewRatio 来指定 ，1:2只是一个大概的值，比如说我分配Xms20M，输出的比例是 6:13.5 ，Xms10M，输出比例为2.5:7，是一个大概的1:2)，即：新生代 ( Young ) = <sup>1</sup>&frasl;<sub>3</sub> 的堆空间大小。老年代 ( Old ) = <sup>2</sup>&frasl;<sub>3</sub> 的堆空间大小。其中，新生代 ( Young ) 被细分为 Eden 和 两个 Survivor 区域，这两个 Survivor 区域分别被命名为 from 和 to，以示区分。
默认的，Eden : from : to = 8 : 1 : 1 ( 可以通过参数 –XX:SurvivorRatio 来设定 )，即： Eden = <sup>8</sup>&frasl;<sub>10</sub> 的新生代空间大小，from = to = <sup>1</sup>&frasl;<sub>10</sub> 的新生代空间大小。
JVM 每次只会使用 Eden 和其中的一块 Survivor 区域来为对象服务，所以无论什么时候，总是有一块 Survivor 区域是空闲着的。
因此，新生代实际可用的内存空间为 <sup>9</sup>&frasl;<sub>10</sub> ( 即90% )的新生代空间。</p>

<p><a href="https://www.cnblogs.com/liuqing576598117/p/10277053.html">JVM老年代和新生代的比例</a></p>
</blockquote>

<ol>
<li>堆溢出处理</li>
</ol>

<p><code>-XX:+HeapDumpOnOutOfMemoryError</code> 在内在溢出时导出整个堆信息</p>

<p><code>-XX:HeapDumpPath</code> 指定导出堆的存放路径</p>

<p><code>-XX:OnOutOfMemoryError</code> 出现OOM时触发操作，用法如下，OOM时调用 printStack.sh脚本。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   <span style="color:#4e9a06">&#34;-XX:OnOutOfMemoryError=/Users/dmn/IdeaProjects/demo/printStack.sh %p&#34;</span></code></pre></div>
<p>测试代码:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java.util.ArrayList</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java.util.List</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">    *
</span><span style="color:#8f5902;font-style:italic">    * -XX:+PrintGCDetails -Xmx20m -Xms5m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=DumpOOM.dump
</span><span style="color:#8f5902;font-style:italic">    * @author dmn
</span><span style="color:#8f5902;font-style:italic">    */</span>
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">DumpOOM</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#000">List</span> <span style="color:#000">l</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">ArrayList</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">100</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
               <span style="color:#000">l</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">add</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">new</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#000">1</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">1024</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">1024</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>            
               <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;分配了1M内存&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   <span style="color:#ce5c00;font-weight:bold">}</span>
   </code></pre></div>
<p>运行结果：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   分配了1M内存
   [GC (Allocation Failure) [PSYoungGen: 765K-&gt;512K(1536K)] 14077K-&gt;13856K(15360K), 0.0006640 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
   [GC (Allocation Failure) [PSYoungGen: 512K-&gt;480K(1536K)] 13856K-&gt;13832K(15360K), 0.0006287 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
   [Full GC (Allocation Failure) [PSYoungGen: 480K-&gt;0K(1536K)] [ParOldGen: 13352K-&gt;13674K(13824K)] 13832K-&gt;13674K(15360K), [Metaspace: 2696K-&gt;2696K(1056768K)], 0.0040420 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 
   [GC (Allocation Failure) [PSYoungGen: 0K-&gt;0K(1536K)] 13674K-&gt;13674K(15360K), 0.0007653 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
   [Full GC (Allocation Failure) [PSYoungGen: 0K-&gt;0K(1536K)] [ParOldGen: 13674K-&gt;13662K(13824K)] 13674K-&gt;13662K(15360K), [Metaspace: 2696K-&gt;2696K(1056768K)], 0.0048541 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
   java.lang.OutOfMemoryError: Java heap space
   Dumping heap to DumpOOM.dump ...
   Heap dump file created [14589700 bytes in 0.025 secs]
   Heap
    PSYoungGen      total 1536K, used 31K [0x00000007bf980000, 0x00000007bfc80000, 0x00000007c0000000)
     eden space 1024K, 3% used [0x00000007bf980000,0x00000007bf987c68,0x00000007bfa80000)
     from space 512K, 0% used [0x00000007bfa80000,0x00000007bfa80000,0x00000007bfb00000)
     to   space 512K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfc80000)
    ParOldGen       total 13824K, used 13662K [0x00000007bec00000, 0x00000007bf980000, 0x00000007bf980000)
     object space 13824K, 98% used [0x00000007bec00000,0x00000007bf957930,0x00000007bf980000)
    Metaspace       used 2727K, capacity 4486K, committed 4864K, reserved 1056768K
     class space    used 292K, capacity 386K, committed 512K, reserved 1048576K</code></pre></div>
<p>以上的运行结果，我没看懂的一点就是，为什么老年代的空间是 13824k，按说，初始化的堆内存是5m，这样新生代的默认内存是1.5M，新老比1：2，老年代就是3M左右，然后程序运行以后，因为新生代的eden区是1024K、from区和to区是512K，理论上装不下 1M的对象，就把对象直接给干到了老年代去了，按输出结果看出来老年代空间是13824k，所以装了13个1M的对象以后，就装不下了，就OOM了，但是有个问题啊，新生代是1.5M，老年代是14M，加起来也才 16M，如果算上那个Metaspace的 2.7M的话，倒是大概能有个20M的内存，但是方法区/元数据是所有线程共享的内存区域，用于保存系统的类信息，类的字段、方法、常量池等。是与堆、栈并列存在的一块内存区域，这块的内存应该不会算在堆内存的20M里面的，那少的那4M左右的内存去哪了呢？后面再把这个坑填上。</p>

<h2 id="五-非堆内存的参数配置">五、非堆内存的参数配置</h2>

<ol>
<li>方法区配置</li>
</ol>

<p>jdk1.6、jdk1.7等版本</p>

<p><code>-XX:PermSize</code> 初始永久区大小</p>

<p><code>-XX:MaxPermSize</code> 配置最大永久区大小</p>

<p>jdk1.8 永久区移除，改为元数据区存放类的元数据，默认情况下，元数据区只受系统可用内存限制，可以使用</p>

<p><code>-XX:MaxMetaspaceSize</code>指定永久区的最大可用值。</p>

<ol>
<li>栈配置</li>
</ol>

<p><code>-Xss</code>指定线程栈大小</p>

<ol>
<li>直接内存配置</li>
</ol>

<p><code>-XX:MaxDirectMemorySize</code>设置最大可用直接内存，如不设置 ，默认值为最大堆空间，即-Xmx，当直接内存使用量达到<code>-XX:MaxDirectMemorySize</code>时，会触发垃圾回收</p>

<p>直接内存适合申请次数较少，访问较频繁的场合，如果内存空间本身需要频繁申请，则不适合使用直接内存。</p>

<p>访问频繁场合的测试代码：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java.nio.ByteBuffer</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">    * -server 模式下 差异明显
</span><span style="color:#8f5902;font-style:italic">    */</span>
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">AccessDirectBuffer</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#000">AccessDirectBuffer</span> <span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">AccessDirectBuffer</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">alloc</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">bufferAccess</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">alloc</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">directAccess</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
           <span style="color:#000">alloc</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">bufferAccess</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">alloc</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">directAccess</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   		<span style="color:#8f5902;font-style:italic">/**直接内存访问*/</span>
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">directAccess</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">starttime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
         	<span style="color:#8f5902;font-style:italic">//申请500个字节的直接内存
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">ByteBuffer</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ByteBuffer</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">allocateDirect</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">500</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">100000</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
               <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">99</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">j</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span>
                 	<span style="color:#8f5902;font-style:italic">//存
</span><span style="color:#8f5902;font-style:italic"></span>                   <span style="color:#000">b</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">putInt</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">j</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
             	<span style="color:#8f5902;font-style:italic">//翻转
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#000">b</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">flip</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
               <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">99</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">j</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span>
                 	<span style="color:#8f5902;font-style:italic">//取
</span><span style="color:#8f5902;font-style:italic"></span>                   <span style="color:#000">b</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">getInt</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
               <span style="color:#000">b</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">clear</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
           <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">endtime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;testDirectWrite:&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">endtime</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">starttime</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   		<span style="color:#8f5902;font-style:italic">/**堆内存访问*/</span>
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">bufferAccess</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">starttime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
         	<span style="color:#8f5902;font-style:italic">//申请 500个字节的内存空间
</span><span style="color:#8f5902;font-style:italic"></span>           <span style="color:#000">ByteBuffer</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ByteBuffer</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">allocate</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">500</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">100000</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
               <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">99</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">j</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span>
                 	<span style="color:#8f5902;font-style:italic">//存
</span><span style="color:#8f5902;font-style:italic"></span>                   <span style="color:#000">b</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">putInt</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">j</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
             	<span style="color:#8f5902;font-style:italic">//翻转
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#000">b</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">flip</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
               <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">j</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">99</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">j</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span>
                 	<span style="color:#8f5902;font-style:italic">//取
</span><span style="color:#8f5902;font-style:italic"></span>                   <span style="color:#000">b</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">getInt</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
               <span style="color:#000">b</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">clear</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
           <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">endtime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;testBufferWrite:&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">endtime</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">starttime</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   <span style="color:#ce5c00;font-weight:bold">}</span>
   </code></pre></div>
<p>频繁申请内存场合测试代码：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java.nio.ByteBuffer</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">    * 直接内存分配较慢
</span><span style="color:#8f5902;font-style:italic">    */</span>
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">AllocDirectBuffer</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#000">AllocDirectBuffer</span> <span style="color:#000">alloc</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">AllocDirectBuffer</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">alloc</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">bufferAllocate</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">alloc</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">directAllocate</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
           <span style="color:#000">alloc</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">bufferAllocate</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">alloc</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">directAllocate</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">directAllocate</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">starttime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">200000</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   	          <span style="color:#8f5902;font-style:italic">//申请直接内存
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#000">ByteBuffer</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ByteBuffer</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">allocateDirect</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">1000</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
           <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">endtime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;directAllocate:&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">endtime</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">starttime</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">bufferAllocate</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">starttime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">200000</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   	          <span style="color:#8f5902;font-style:italic">//申请堆内存
</span><span style="color:#8f5902;font-style:italic"></span>               <span style="color:#000">ByteBuffer</span> <span style="color:#000">b</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">ByteBuffer</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">allocate</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">1000</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
           <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">endtime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;bufferAllocate:&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">endtime</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">starttime</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   <span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div>
<h2 id="六-虚拟机工作模式">六、虚拟机工作模式</h2>

<p>虚拟机系统会根据当前计算机环境自动选择运行模式，使用<code>-version</code>参数可以查看当前的模式</p>

<p><code>-client</code> Client模式</p>

<p><code>-server</code> Server模式，与Client模式比， Server模式启动比较慢，会收集更多系统性能信息，使用更复杂的优化算法对程序进行优化。系统启动后执行速度远快于Client模式。</p>

<p>使用 <code>-XX:+PrintFlagsFinal</code> 参数查看Client模式和Server模式给定的默认参数，如以下可以看到，我的mac电脑貌似 <code>-server</code> 和<code>-client</code>都是用Server模式运行的。64位系统中虚拟机更倾向于使用Server模式运行。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">~% java -XX:+PrintFlagsFinal -server -version <span style="color:#000;font-weight:bold">|</span> grep -E <span style="color:#4e9a06">&#39; CompileThreshold|MaxHeapSize&#39;</span>
     intx <span style="color:#000">CompileThreshold</span>                          <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">10000</span>                               <span style="color:#ce5c00;font-weight:bold">{</span>pd product<span style="color:#ce5c00;font-weight:bold">}</span>
    uintx MaxHeapSize                              :<span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">4294967296</span>                          <span style="color:#ce5c00;font-weight:bold">{</span>product<span style="color:#ce5c00;font-weight:bold">}</span>
java version <span style="color:#4e9a06">&#34;1.8.0_241&#34;</span>
Java<span style="color:#ce5c00;font-weight:bold">(</span>TM<span style="color:#ce5c00;font-weight:bold">)</span> SE Runtime Environment <span style="color:#ce5c00;font-weight:bold">(</span>build 1.8.0_241-b07<span style="color:#ce5c00;font-weight:bold">)</span>
Java HotSpot<span style="color:#ce5c00;font-weight:bold">(</span>TM<span style="color:#ce5c00;font-weight:bold">)</span> 64-Bit Server VM <span style="color:#ce5c00;font-weight:bold">(</span>build 25.241-b07, mixed mode<span style="color:#ce5c00;font-weight:bold">)</span>


~% java -XX:+PrintFlagsFinal -client -version <span style="color:#000;font-weight:bold">|</span> grep -E <span style="color:#4e9a06">&#39; CompileThreshold|MaxHeapSize&#39;</span>
     intx <span style="color:#000">CompileThreshold</span>                          <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">10000</span>                               <span style="color:#ce5c00;font-weight:bold">{</span>pd product<span style="color:#ce5c00;font-weight:bold">}</span>
    uintx MaxHeapSize                              :<span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">4294967296</span>                          <span style="color:#ce5c00;font-weight:bold">{</span>product<span style="color:#ce5c00;font-weight:bold">}</span>
java version <span style="color:#4e9a06">&#34;1.8.0_241&#34;</span>
Java<span style="color:#ce5c00;font-weight:bold">(</span>TM<span style="color:#ce5c00;font-weight:bold">)</span> SE Runtime Environment <span style="color:#ce5c00;font-weight:bold">(</span>build 1.8.0_241-b07<span style="color:#ce5c00;font-weight:bold">)</span>
Java HotSpot<span style="color:#ce5c00;font-weight:bold">(</span>TM<span style="color:#ce5c00;font-weight:bold">)</span> 64-Bit Server VM <span style="color:#ce5c00;font-weight:bold">(</span>build 25.241-b07, mixed mode<span style="color:#ce5c00;font-weight:bold">)</span></code></pre></div>
    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/jvm%E5%AD%A6%E4%B9%A002-%E8%99%9A%E6%8B%9F%E6%9C%BA%E7%9A%84%E5%9F%BA%E6%9C%AC%E7%BB%93%E6%9E%84/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/jvm%E5%AD%A6%E4%B9%A004-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%A6%82%E5%BF%B5%E4%B8%8E%E7%AE%97%E6%B3%95/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">172</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">10</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">96</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2022</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>